package tech.chauncy.springbootwebsample.entity;

import java.util.*;

/**
 * Created by sunjia on 2018/3/6.
 */
public class ProcessTestEntity {

    private String tel;
    private List<String> inputs;
    private List<String> replys;
    private List<OutputsEntity> outputs;


    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public List<String> getInputs() {
        return inputs;
    }

    public void setInputs(List<String> inputs) {
        this.inputs = inputs;
    }

    public List<String> getReplys() {
        return replys;
    }

    public void setReplys(List<String> replys) {
        this.replys = replys;
    }

    public List<OutputsEntity> getOutputs() {
        return outputs;
    }

    public void setOutputs(List<OutputsEntity> outputs) {
        this.outputs = outputs;
    }

    public OutputsEntity getOutputEntityByName(String name){
        OutputsEntity outputsEntity = new OutputsEntity();
        List<OutputsEntity> entities = new ArrayList<>();
        for(OutputsEntity entity : outputs){
            if(entity.getName().equals(name)){
                entities.add(entity);
            }
        }
        List<ConditionEntity> conditionLs = combineOutputsEntityformRepeatAPIName(entities);
        outputsEntity.setName(name);
        outputsEntity.setConditions(conditionLs);
        if (conditionLs.size()==0)
            return null;
        return outputsEntity;
    }

    private List<ConditionEntity> combineOutputsEntityformRepeatAPIName(List<OutputsEntity> entities){

        List<ConditionEntity> conditionLs = new ArrayList<>();
        Map<String, String> conditionStrMap = new HashMap<>();
        for (OutputsEntity outputsEntity : entities){
            for(ConditionEntity condition : outputsEntity.getConditions()){
                String conditionStr = condition.getCondition();
                String conditionResult = condition.getResult();
                if (!conditionStrMap.containsKey(conditionStr)){
                    conditionStrMap.put(conditionStr,conditionResult);
                }else{
                    conditionStrMap.put(conditionStr,conditionResult+","+conditionStrMap.get(conditionStr));
                }
            }
        }
        for(String key : conditionStrMap.keySet()){
            ConditionEntity conditonTmp = new ConditionEntity();
            conditonTmp.setResult(conditionStrMap.get(key));
            conditonTmp.setCondition(key);
            conditionLs.add(conditonTmp);
        }
        return conditionLs;

    }

    public ConditionEntity getConditionEntityByNameAndCondition(String name, String condition){
        OutputsEntity entity = getOutputEntityByName(name);
        for(ConditionEntity con : entity.getConditions()){
            if(con.getCondition().equals(condition)){
                return con;
            }
        }
        return null;
    }

    public List<String> getConditionStrListByName(String name){
        List<String> conditionStrList = new ArrayList<>();
        List<ConditionEntity> conditionEntityList = getOutputEntityByName(name).getConditions();
        for(ConditionEntity conditionEntity : conditionEntityList){
            conditionStrList.add(conditionEntity.getCondition());
        }
        return conditionStrList;
    }

}
